package cn.ges.movie.vo.mv

import cn.ges.base.sta.vo.base.BgSimpleModifyVO
import cn.ges.movie.entity.MvScheduling
import cn.ges.movie.mapper.MovieMapper
import cn.ges.spring.SpringContextUtil
import com.aliyun.oss.common.utils.DateUtil
import java.math.BigDecimal
import java.time.Instant
import java.time.LocalDateTime

open class MmdVO : BgSimpleModifyVO<MvScheduling>() {

    var id: Long? = null

    /**
     * 电影厅ID
     */
    var mhId: Long? = null

    /**
     * 开始时间  2021-04-18 00:00:00
     */
    var startTime: String? = null

    var tStartTime: LocalDateTime? = null

    var tTime: Int? = null

    /**
     * 电影ID
     */
    var mvId: Long? = null

    /**
     * 该场价格
     */
    var price: BigDecimal? = null

    override fun check(): Boolean {
        if (mhId == null || startTime == null || mvId == null || price == null) {
            return false
        }
        this.tStartTime = cn.ges.date.DateUtil.stringToTimeStamp(this.startTime)
        val beanByClass = SpringContextUtil.getBeanByClass(MovieMapper::class.java)
        val selectById = beanByClass.selectById(mvId)
        if (selectById == null) {
            errorMsg = "不存在的电影"
            return false
        } else {
            tTime = selectById.time!!
        }

        return true
    }

    override fun cast2Entity(): MvScheduling {
        val mmdVO = this
        return MvScheduling().apply {
            this.id = mmdVO.id
            this.mhId = mmdVO.mhId
            this.startTime = cn.ges.date.DateUtil.localDateTime2Instant(mmdVO.tStartTime!!)
            this.endTime =cn.ges.date.DateUtil.localDateTime2Instant(mmdVO.tStartTime!!.plusMinutes(tTime!!.toLong()))
            this.mvId = mmdVO.mvId
            this.price = mmdVO.price
        }
    }
}